package com.imd.s9.impl;

import com.imd.base.BusinessException;
import com.imd.s9.entity.TableInfo;
import com.imd.s9.service.FinanceService;
import com.imd.s9.util.InitCriteria;
import org.bson.Document;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.print.Doc;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class FinanceImpl implements FinanceService {

    @Resource
    InitCriteria initCriteria;
    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public void upsertProjectCost(String tenantId,String templateId, Map entity) throws BusinessException{
        if(ObjectUtils.isEmpty(entity.get("项目名称"))){return;}
        if(ObjectUtils.isEmpty(entity.get("单据编号"))){return;}
        String projectName = entity.get("项目名称").toString();

        Query query = initCriteria.create(tenantId,Criteria.where("单据编号").is(entity.get("单据编号")));
        Document doc = mongoTemplate.findOne(query,Document.class, "entity_"+templateId);

        Query query1 = initCriteria.create(tenantId,Criteria.where("项目名称").is(entity.get("项目名称")));
        Document doc1 = mongoTemplate.findOne(query1,Document.class, TableInfo.projectCost);
        Update update = new Update();
        switch (templateId){
            /**销售订单*/
            case "66b48b83ac1bb2657a2c92e7":
                if(doc1==null||doc1.isEmpty()){
                    List<Document> list = new ArrayList();
                    Document temp= createDoc(doc);
                    list.add(temp);
                    update.set("项目名称",projectName);
                    update.set("销售单",list);
                    update.set("销售总额",temp.get("订单总额"));
                    update.set("当前盈单",temp.get("订单总额"));
                }else {
                    List<Document> orders = doc1.getList("销售单",Document.class);
                    Document temp= createDoc(doc);
                    orders.add(temp);
                    BigDecimal bg = new BigDecimal(0);
                    for(Document order:orders){
                        bg = bg.add(new BigDecimal(order.getDouble("订单总额")));
                    }
                    update.set("销售单",orders);
                    update.set("销售总额",bg.doubleValue());
                    BigDecimal nowBg = new BigDecimal(doc1.get("当前盈单")==null?0:doc1.getDouble("当前盈单"));
                    update.set("当前盈单",nowBg.add(new BigDecimal(temp.getDouble("订单总额"))).doubleValue());
                }
                break;

            /**采购单*/
            case "66b6cfbff13c9b26892a39c5":
                if(doc1!=null&&!doc1.isEmpty()){
                    List<Document> orders = (doc1.get("采购单")==null||doc1.getList("采购单",Document.class).isEmpty())?
                            new ArrayList<>():doc1.getList("采购单",Document.class);
                    Document temp= createDoc(doc);
                    orders.add(temp);
                    BigDecimal bg = new BigDecimal(0);
                    for(Document order:orders){
                        bg = bg.add(new BigDecimal(order.getDouble("订单总额")));
                    }
                    update.set("采购单",orders);
                    update.set("采购总额",bg.doubleValue());
                    BigDecimal nowBg = new BigDecimal(doc1.get("当前盈单")==null?0:doc1.getDouble("当前盈单"));
                    update.set("当前盈单",nowBg.subtract(new BigDecimal(temp.getDouble("订单总额"))).doubleValue());
                }
                break;

            /**委外单*/
            case "3":
                if(doc1!=null&&!doc1.isEmpty()){
                    List<Document> orders = (doc1.get("委外单")==null||doc1.getList("委外单",Document.class).isEmpty())?
                            new ArrayList<>():doc1.getList("委外单",Document.class);
                    Document temp= createDoc(doc);
                    orders.add(temp);
                    BigDecimal bg = new BigDecimal(0);
                    for(Document order:orders){
                        bg = bg.add(new BigDecimal(order.getDouble("订单总额")));
                    }
                    update.set("委外单",orders);
                        update.set("委外总额",bg.doubleValue());
                    BigDecimal nowBg = new BigDecimal(doc1.get("当前盈单")==null?0:doc1.getDouble("当前盈单"));
                    update.set("当前盈单",nowBg.subtract(new BigDecimal(temp.getDouble("订单总额"))).doubleValue());
                }
                break;

            /**领用退料单*/
            case "4":
                if(doc1!=null&&!doc1.isEmpty()){
                    List<Document> orders = (doc1.get("领用退料单")==null||doc1.getList("领用退料单",Document.class).isEmpty())?
                            new ArrayList<>():doc1.getList("领用退料单",Document.class);
                    Document temp= createDoc(doc);
                    orders.add(temp);
                    BigDecimal bg = new BigDecimal(0);
                    for(Document order:orders){
                        bg = bg.add(new BigDecimal(order.getDouble("订单总额")));
                    }
                    update.set("领用退料单",orders);
                    update.set("领料总额",bg.doubleValue());
                    BigDecimal nowBg = new BigDecimal(doc1.get("当前盈单")==null?0:doc1.getDouble("当前盈单"));
                    if(doc.getBoolean("单据类型")){
                        update.set("当前盈单",nowBg.add(new BigDecimal(temp.getDouble("订单总额"))).doubleValue());
                    }else {
                        update.set("当前盈单",nowBg.subtract(new BigDecimal(temp.getDouble("订单总额"))).doubleValue());
                    }
                }
                break;
            default:
                break;
        }
        mongoTemplate.upsert(query1,update,TableInfo.projectCost);
    }

    @Override
    public void createProjectCost(String tenantId,String projectName) throws BusinessException {
        Query query = initCriteria.create(tenantId,Criteria.where("项目名称").is(projectName));
        Update update = new Update();
        update.set("项目名称",projectName);
        BigDecimal profit = new BigDecimal(0);
        /**销售单处理*/
        List<Document> saleOs= mongoTemplate.find(query,Document.class, TableInfo.saleO);
        if(saleOs==null||saleOs.isEmpty()){
            throw new BusinessException("该项目不存在销售订单");
        }else {
            List<Document> orders = new ArrayList<>();
            BigDecimal bg = new BigDecimal(0);
            for(Document doc:saleOs){
                Document temp= createDoc(doc);
                orders.add(temp);
                bg = bg.add(new BigDecimal(temp.getDouble("订单总额")));
            }
            update.set("销售单",orders);
            update.set("销售总额",bg.doubleValue());
            profit = profit.add(bg);
        }

        /**采购单处理*/
        List<Document> purchaseOs = mongoTemplate.find(query,Document.class, TableInfo.purchaseO);
        if(purchaseOs!=null&&!purchaseOs.isEmpty()){
            List<Document> orders = new ArrayList<>();
            BigDecimal bg = new BigDecimal(0);
            for(Document doc:purchaseOs){
                Document temp= createDoc(doc);
                orders.add(temp);
                bg = bg.add(new BigDecimal(temp.getDouble("订单总额")));
            }
            update.set("采购单",orders);
            update.set("采购总额",bg.doubleValue());
            profit = profit.subtract(bg);
        }

//        /**委外单处理*/
//        List<Document> outsourceOs = mongoTemplate.find(query,Document.class, TableInfo.outsourceO);
//        if(outsourceOs!=null&&!outsourceOs.isEmpty()){
//            List<Document> orders = new ArrayList<>();
//            BigDecimal bg = new BigDecimal(0);
//            for(Document doc:outsourceOs){
//                Document temp= createDoc(doc);
//                orders.add(temp);
//                bg = bg.add(new BigDecimal(temp.getDouble("订单总额")));
//            }
//            update.set("委外单",orders);
//            update.set("委外总额",bg.doubleValue());
//            profit = profit.subtract(bg);
//        }

        /**领料退料单*/
        List<Document> returnMaterialOs = mongoTemplate.find(query,Document.class, TableInfo.returnMaterialO);
        if(returnMaterialOs!=null&&!returnMaterialOs.isEmpty()){
            List<Document> orders = new ArrayList<>();
            BigDecimal bg = new BigDecimal(0);
            for(Document doc:returnMaterialOs){
                Document temp= createDoc(doc);
                orders.add(temp);
                if(doc.getBoolean("单据类型")){
                    bg = bg.subtract(new BigDecimal(temp.getDouble("订单总额")));
                }else {
                    bg = bg.add(new BigDecimal(temp.getDouble("订单总额")));
                }
            }
            update.set("领用退料单",orders);
            update.set("领料总额",bg.doubleValue());
            profit = profit.subtract(bg);
        }
        update.set("当前盈单",profit.doubleValue());
        mongoTemplate.upsert(query,update,TableInfo.projectCost);
    }

    /**
     * 单据列表item
     * @param doc
     * @return
     */
    Document createDoc (Document doc){
        Document temp = new Document();
        temp.put("订单编号",createOrder(doc));
        temp.put("订单总额",calculate(doc));
        temp.put("订单时间",doc.getDate("createTime"));
        return temp;
    }

    /**
     * 计算单据明细总价
     * @param doc
     * @return
     */
    Double calculate(Document doc) {
        BigDecimal bigDecimal = new BigDecimal(0);
        List<Document> list = doc.getList("明细",Document.class);
        if(list!=null&&!list.isEmpty()){
            for(Document document:list){
                bigDecimal= bigDecimal.add(new BigDecimal(document.get("总价")==null?0:document.getDouble("总价")));
            }
        }
        return bigDecimal.doubleValue();
    }

    Document createOrder(Document doc){
        Document d = new Document();
        d.put("订单编号",doc.get("订单编号"));
        d.put("name",doc.get("订单编号"));
        return d;
    }


}
